package net.wangds.dymodel.comp;

import net.wangds.dymodel.meta.entity.DyModelDefEntity;
import net.wangds.dymodel.meta.repo.db.DyModelDefRepo;
import net.wangds.jdbc.comp.jdbc.JdbcComp;
import net.wangds.jpa.comp.AbstractRemovedComp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * .
 * <p></p>
 *
 * @author 王东石 2020/11/10 15:59.
 */
@Component
public class DyModelDefOpComp extends AbstractRemovedComp<DyModelDefEntity> {

    @Autowired
    private JdbcComp mmJdbc;

    /**
     * 构造函数.
     *
     * @param repo 数据库操作对象.
     */
    @Autowired
    protected DyModelDefOpComp(DyModelDefRepo repo) {
        super(repo);
    }

    @Override
    public Page<DyModelDefEntity> findByKeywords(String keywords, Pageable paging) {
        String sql = "SELECT t1.*\n" +
                "FROM dy_model_def t1\n" +
                "where removed = 0 and (\n" +
                "  t1.name_cn regexp ? or t1.name_en regexp ? or t1.remark regexp ?" +
        ")";
        return mmJdbc.executeQuery(sql, new Object[]{keywords, keywords, keywords}, paging, DyModelDefEntity.class);
    }

    public Optional<DyModelDefEntity> findById(String id) {
        return ((DyModelDefRepo)this.repo).findByIdOrNameEnOrNameCn(id,id,id);
    }

    public boolean existsById(String id) {

        return ((DyModelDefRepo)this.repo).existsByIdOrNameEnOrNameCn(id, id, id);
    }

}
